<template>
  <el-form
    :model="formData"
    label-width="100px"
    label-suffix=":"
    :show-message="false"
    ref="formRef"
  >
    <el-form-item label="是否通过" prop="status">
      <el-radio-group v-model="formData.status">
        <el-radio :label="0">通过</el-radio>
        <el-radio :label="1">不通过</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item
      label="请填写原因"
      prop="remark"
      :rules="formData.status == 1 ? formRules.remark : []"
    >
      <el-input
        v-model="formData.remark"
        type="textarea"
        :rows="4"
        placeholder="请输入原因"
      />
    </el-form-item>
    <el-row>
      <el-col :span="24" align="center">
        <el-button type="primary" @click="onSubmit">保存</el-button>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
// 审批表单
export default {
  props: {
    approvalData: {
      type: Object,
      default: () => ({}),
    },
    code: {
      type: String,
      default: 'id',
    },
  },
  data() {
    return {
      formData: {
        status: 0,
        remark: '',
      },
      formRules: {
        remark: [{ required: true, message: '请输入原因' }],
      },
    }
  },
  methods: {
    onSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          if (this.code !== 'id') {
            this.$emit('save', {
              ...this.formData,
              sourceCode: this.approvalData[this.code],
            })
          } else {
            this.$emit('save', {
              ...this.formData,
              code: this.approvalData.id,
            })
          }
        }
      })
    },
  },
  mounted() {},
}
</script>
<style lang="scss" scoped></style>
